// 水印功能
class Watermark {
    constructor(dom, settings) {
        this.dom = dom;
        this.defaultSettings = {
            watermark_txt0: "山东高速集团有限公司",
            watermark_txt1: "",
            watermark_txt2: "", //根据本业务需求  有三行 换行显示  创建三个节点
            watermark_x: 20, //水印起始位置x轴坐标
            watermark_y: 20, //水印起始位置Y轴坐标
            watermark_rows: 20, //水印行数
            watermark_cols: 20, //水印列数
            watermark_x_space: 50, //水印x轴间隔
            watermark_y_space: 50, //水印y轴间隔
            watermark_color: '#aaa', //水印字体颜色
            watermark_alpha: 0.4, //水印透明度
            watermark_fontsize: '16px', //水印字体大小
            watermark_font: '微软雅黑', //水印字体
            watermark_width: 180, //水印宽度
            watermark_height: 60, //水印长度
            watermark_angle: 5 //水印倾斜度数
        }
        if (settings && typeof settings === "object") {
            let src = settings || {};
            for (let key in src) {
                if (src[key] && this.defaultSettings[key] && src[key] === this.defaultSettings[key]) {
                    continue;
                } else if (src[key]){
                    this.defaultSettings[key] = src[key];
                }
            }
        }
    }
    setWatermark() { // 创建水印
        let oTemp = document.createDocumentFragment(); // 虚拟节点
        //获取页面最大宽度
        let page_width = Math.max(this.dom.scrollWidth, this.dom.clientWidth);
        let cutWidth = page_width * 0.0150;
        page_width = page_width - cutWidth;
        //获取页面最大高度
        // let page_height = Math.max(this.dom.scrollHeight, this.dom.clientHeight) + 450;
        let page_height = this.dom.scrollHeight + this.dom.scrollTop;
        //如果将水印列数设置为0，或水印列数设置过大，超过页面最大宽度，则重新计算水印列数和水印x轴间隔
        if (this.defaultSettings.watermark_cols == 0 || (parseInt(this.defaultSettings.watermark_x + this.defaultSettings.watermark_width * this.defaultSettings.watermark_cols + this.defaultSettings.watermark_x_space * (this.defaultSettings.watermark_cols - 1)) > page_width)) {
            this.defaultSettings.watermark_cols = parseInt((page_width - this.defaultSettings.watermark_x + this.defaultSettings.watermark_x_space) / (this.defaultSettings.watermark_width + this.defaultSettings.watermark_x_space));
            this.defaultSettings.watermark_x_space = parseInt((page_width - this.defaultSettings.watermark_x - this.defaultSettings.watermark_width * this.defaultSettings.watermark_cols) / (this.defaultSettings.watermark_cols - 1));
        }
        //如果将水印行数设置为0，或水印行数设置过大，超过页面最大长度，则重新计算水印行数和水印y轴间隔
        if (this.defaultSettings.watermark_rows == 0 || (parseInt(this.defaultSettings.watermark_y + this.defaultSettings.watermark_height * this.defaultSettings.watermark_rows + this.defaultSettings.watermark_y_space * (this.defaultSettings.watermark_rows - 1)) > page_height)) {
            this.defaultSettings.watermark_rows = parseInt((this.defaultSettings.watermark_y_space + page_height - this.defaultSettings.watermark_y) / (this.defaultSettings.watermark_height + this.defaultSettings.watermark_y_space));
            this.defaultSettings.watermark_y_space = parseInt(((page_height - this.defaultSettings.watermark_y) - this.defaultSettings.watermark_height * this.defaultSettings.watermark_rows) / (this.defaultSettings.watermark_rows - 1));
        }
        let x;
        let y;
        for (let i = 0; i < this.defaultSettings.watermark_rows; i++) {
            y = this.defaultSettings.watermark_y + (this.defaultSettings.watermark_y_space + this.defaultSettings.watermark_height) * i;
            for (let j = 0; j < this.defaultSettings.watermark_cols; j++) {
                x = this.defaultSettings.watermark_x + (this.defaultSettings.watermark_width + this.defaultSettings.watermark_x_space) * j;
                let mask_div = document.createElement('div');
                mask_div.id = 'mask_div' + i + j;
                mask_div.className = 'mask_div';
                ///三个节点
                let span0 = document.createElement('div');
                span0.appendChild(document.createTextNode(this.defaultSettings.watermark_txt0));
                let span1 = document.createElement('div');
                span1.appendChild(document.createTextNode(this.defaultSettings.watermark_txt1));
                let span2 = document.createElement('div');
                span2.appendChild(document.createTextNode(this.defaultSettings.watermark_txt2));
                mask_div.appendChild(span0);
                mask_div.appendChild(span1);
                mask_div.appendChild(span2);
                //设置水印div倾斜显示
                mask_div.style.webkitTransform = "rotate(-" + this.defaultSettings.watermark_angle + "deg)";
                mask_div.style.MozTransform = "rotate(-" + this.defaultSettings.watermark_angle + "deg)";
                mask_div.style.msTransform = "rotate(-" + this.defaultSettings.watermark_angle + "deg)";
                mask_div.style.OTransform = "rotate(-" + this.defaultSettings.watermark_angle + "deg)";
                mask_div.style.transform = "rotate(-" + this.defaultSettings.watermark_angle + "deg)";
                mask_div.style.visibility = "";
                mask_div.style.position = "absolute";
                mask_div.style.left = x + 'px';
                mask_div.style.top = y + 'px';
                mask_div.style.overflow = "hidden";
                mask_div.style.zIndex = "999";
                mask_div.style.pointerEvents = 'none'; // pointer-events:none  让水印不遮挡页面的点击事件
                mask_div.style.padding = "15px 0 0 0";
                mask_div.style.opacity = this.defaultSettings.watermark_alpha;
                mask_div.style.fontSize = this.defaultSettings.watermark_fontsize;
                mask_div.style.fontFamily = this.defaultSettings.watermark_font;
                mask_div.style.color = this.defaultSettings.watermark_color;
                mask_div.style.textAlign = "center";
                mask_div.style.width = this.defaultSettings.watermark_width + 'px';
                mask_div.style.height = this.defaultSettings.watermark_height + 'px';
                mask_div.style.display = "block";
                oTemp.appendChild(mask_div);
            };
        };
        this.dom.appendChild(oTemp);
    }
    clearWatermark() { // 清空水印
        let mask_divs = this.dom.querySelectorAll('.mask_div');
        mask_divs.forEach((item) => {
            this.dom.removeChild(item);
        })
    }
    haveWatermark() {
        let mask_divs = this.dom.querySelectorAll('.mask_div');
        if (mask_divs.length > 0) return true;
        else return false;
    }
}

export default Watermark;
